package com.arpa.wms.cancel.delivery;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.json.JSONUtil;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.wms.cancel.DeliveryCancelHandlerTypeConstants;
import com.arpa.wms.domain.consts.NumConst;
import com.arpa.wms.domain.dto.PickItemCancelQuantityDto;
import com.arpa.wms.domain.entity.Delivery;
import com.arpa.wms.domain.entity.Picking;
import com.arpa.wms.mapper.DeliveryMapper;
import com.arpa.wms.service.IDeliveryService;
import com.arpa.wms.service.IPickingItemService;
import com.arpa.wms.service.IPickingRegisterService;
import com.arpa.wms.service.IPickingService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 待拣货订单取消处理
 * 1、更新订单状态作废；
 * 2、分配库存退回原库位；
 * 3、更新拣货单取消数量
 *
 * @author leo
 */
@Log4j2
@Component(DeliveryCancelHandlerTypeConstants.UN_PICKING)
public class UnPickingCancelHandler extends DistributionedCancelHandler {

    private final IPickingService pickingService;
    private final IPickingItemService pickingItemService;
    private final IPickingRegisterService pickingRegisterService;
    @Resource
    private DeliveryMapper deliveryMapper;

    public UnPickingCancelHandler(IPickingService pickingService,
                                  IPickingItemService pickingItemService,
                                  IPickingRegisterService pickingRegisterService
                                  ) {
        this.pickingService = pickingService;
        this.pickingItemService = pickingItemService;
        this.pickingRegisterService = pickingRegisterService;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean handle(IDeliveryService deliveryService, Delivery delivery) {
        log.info("待拣货订单取消处理。订单号：{}，波次单号：{}，订单信息：{}", delivery.getCode(), delivery.getWavePickingCode(), JSONUtil.toJsonStr(delivery));
        boolean bool = super.handle(deliveryService, delivery);
        //更新拣货单取消数量和状态
        BigDecimal cancelQuantity = delivery.getDistributionNum();
        if (cancelQuantity.compareTo(BigDecimal.ZERO) > NumConst.NUM_ZERO) {
            updatePickingCancelQuantityAndStatus(delivery, cancelQuantity);
        }
        return bool;
    }

    /**
     * 更新拣货单取消数量和状态
     * @author liuxr
     * @date 2021/12/9 19:06
     * @param delivery
     * @param cancelQuantity
     */
    public void updatePickingCancelQuantityAndStatus(Delivery delivery, BigDecimal cancelQuantity) {
        Picking picking = pickingService.getOne(Wrappers.lambdaQuery(Picking.class)
                .select(Picking::getCode)
                .eq(Picking::getWavePickingCode, delivery.getWavePickingCode()));
        String pickCode = picking.getCode();
        //更新拣货单取消数量
        pickingService.updateCancelQuantityAndStatusByCode(pickCode, cancelQuantity);


        List<String> codes = new ArrayList<>();
        codes.add(delivery.getCode());
        List<Map<String, Object>> list = deliveryMapper.getDeliveryItemGoodsSortQuantity(codes, UserUtil.getBranchCode());
        Map<String, String> fieldMapping = new HashMap<>();
        fieldMapping.put("goods_code","goodsCode");
        fieldMapping.put("distribution_quantity","cancelQuantity");
        CopyOptions copyOptions = new CopyOptions();
        copyOptions.setFieldMapping(fieldMapping);
        List<PickItemCancelQuantityDto> pickItemCancelQuantityDtoList = new ArrayList<>();
        for (Map<String, Object> r : list) {
            PickItemCancelQuantityDto pickItemCancelQuantityDto = BeanUtil.toBean(r, PickItemCancelQuantityDto.class, copyOptions);
            pickItemCancelQuantityDto.setPickingCode(pickCode);
            pickItemCancelQuantityDtoList.add(pickItemCancelQuantityDto);
        }
        //更新拣货单明细取消数量
        pickingItemService.updateCancelQuantityBatch(pickItemCancelQuantityDtoList);
        pickingRegisterService.updateCancelQuantityBatch(pickItemCancelQuantityDtoList);

    }


}
